﻿using System;
using System.Data;
using System.Diagnostics;
using System.Linq;
using Dapper;
using MySql.Data.MySqlClient;

namespace DataBaseLayer
{
    /// <summary>
    /// https://github.com/antonheryanto/dapper-rainbow-mysql
    /// 
    /// http://www.lanhusoft.com/Article/520.html
    /// 
    /// http://blog.darkthread.net/post-2014-05-15-dapper.aspx
    /// 
    /// https://github.com/StackExchange/Dapper/releases
    /// 
    /// dapper操作连接mysql开发最完整实例总结
    /// http://www.lanhusoft.com/Article/520.html
    /// </summary>
    public class Demo
    {
        public static Db DbInit()
        {
            string connectionString = "server = 127.0.0.1; database = photon; uid = root; pwd =123456; charset = 'gbk'";

            MySqlConnection connection = new MySqlConnection(connectionString);
            connection.Open();
            return Db.Init(connection, commandTimeout: 30);
        }


        public void Test()
        {

            //Guid.NewGuid().ToString()

            Db aa = DbInit();

            
           var a3 = aa.Query<device>("select * from device where uuid=@uuid", new { uuid = "aabb636238694579824398" });

           

            device dv = new device
            {
                UUID = "aabb" + DateTime.Now.Ticks.ToString(),
                DeviceModel = "IOS",
                DeviceName = "Iphone 5s",
                RegisterDate = DateTime.Now.AddDays(-1),
                UpdateDate = DateTime.Now,
                TokenID = DateTime.Now.Ticks.ToString()
            };

            //'test.device' 
            long ret = aa.Devices.InsertOrUpdate(dv);

            var a1 = aa.Devices.All();

            var a2 = aa.Query<device>("select * from device");
            //            Get All The Users

            //IEnumerable<User> all = db.Users.All();
            //            Get A User

            //            User user = db.Users.Get(userId);
            //            User same_user = db.Users.Get(new { Id = userId });
            //            Delete a User

            //bool isTrue = db.Users.Delete(user);
            //            bool isAnotherTrue = db.Users.Delete(new { Id = userId });
            //            Get The First User

            //User user = db.Users.First();
            //            Insert A User

            //long uid = db.Users.Insert(
            //  new
            //  {
            //      Email = "foolio@coolio.com",
            //      Name = "Foolio Coolio",
            //      Password = "AHashedPasswordOfLengthThirtyTwo"
            //  });
            //            Insert Or Update A User

            //int uid = db.Users.InsertOrUpdate(user);
            //            Update

            //user.Name = "Foolio Jr."
            //int uid = db.Users.Update(uid, user);
            //            int uid = db.Users.Update(new { Id = uid }, user);


        }

    }

    public class Db : Database<Db>
    {
        public Table<device> Devices { get; set; }

        public Table<devicelog> DeviceLog { get; set; }

        public Table<devicechat> DeviceChat { get; set; }
    }
}
